HTTP requester to call Graphql APIs

 

There is no MuleSoft certified connector available to call Graphql APIs in MuleSoft. In case if we come across a requirement to call Graphql endpoint in MuleSoft then we can leverage the existing HTTP Requestor to call Graphql APIs. Graphql calls are internally a POST call over HTTP.

For the demo we can call below graphql endpoint

Endpoint

https://swapi-graphql.netlify.app/.netlify/functions/index

Query payload

query Query($first: Int) {
  allFilms (last: $first) {
    films{
      title
      director
      releaseDate
      speciesConnection {
        species {
          name
          classification
          homeworld {
            name
          }
        }
      }
    }
  }
}

 

Graphql Variable

{
    "first": 1
}

 

We can see the below postman call to see Graphql endpoint in action

HTTP Requestor to call Graphql APIs

 

To show Graphql endpoint call using HTTP POST

HTTP POST payload

{
	"query": "query Query($first: Int) {\r\n  allFilms (last: $first) {\r\n    films{\r\n      title\r\n      director\r\n      releaseDate\r\n      speciesConnection {\r\n        species {\r\n          name\r\n          classification\r\n          homeworld {\r\n            name\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\n",
	"variables": {
		"first": 1
	}
}

 

POSTMAN call

HTTP Requestor to call Graphql APIs

 

Let’s do the same in MuleSoft

Create a project in Anypoint studio

HTTP Requestor to call Graphql APIs

 

Add the transformation to create the POST payload

HTTP Requestor to call Graphql APIs

 

Create the required HTTP requestor

 

Add logger to log the response

 

Now run the application and hit the http listener endpoint i.e., http://localhost:8081/graphql

 

We can see the logged message in Mule application

 

Sample Application – graphql-service

Sample POSTMAN collection – GraphQL.postman_collection.json

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
aizaz
aizaz
1 year ago

how did you convert the graphql query to the string for use in dataweave ?